package com.zhuiyun.project.api.system.dict.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhuiyun.project.annotation.OptLog;
import com.zhuiyun.project.api.system.dict.entity.DictType;
import com.zhuiyun.project.api.system.dict.service.DictTypeService;
import com.zhuiyun.project.common.controller.BaseController;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.constants.CommonConstants;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName DictController
 * @Description TODO 字典管理控制层
 * @Author gpd
 * @Date 2023-06-15 下午 04:13
 **/
@RestController
@Api(tags = "字典类型管理")
@Slf4j
@CrossOrigin
@RequestMapping("/dictType")
public class DictTypeController extends BaseController {

    @Autowired
    DictTypeService dictTypeService;

    /**
    * @Author gpd
    * @Description 查询字典管理类型
    * @Date 2023-06-15 下午 05:05
    * @Return PageResult

    */
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @ApiOperation(value = "查询字典管理类型")
    @OptLog(modular = "字典类型管理", type = CommonConstants.SELECT, describe = "查询字典管理类型")
    @GetMapping("/list")
    public CommonResult listType(PageRequest pageRequest, DictType dictType){
        IPage<DictType> list = dictTypeService.listType(pageRequest, dictType);
        return CommonResult.ok(list);
    }

    /**
    * @Author gpd
    * @Description 新增字典类型
    * @Date 2023-06-17 上午 09:45
    * @Return com.zhuiyun.project.common.response.CommonResult

    */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @ApiOperation(value = "新增字典类型")
    @OptLog(modular = "字典类型管理", type = CommonConstants.ADD, describe = "新增字典类型")
    @PostMapping("/addDictType")
    public CommonResult addDictType(@RequestBody DictType dictType){
        if(!dictTypeService.checkDictTypeUnique(dictType)){
//            return CommonResult.error("新增字典'" + dictType.getDictName() + "'失败，字典类型已存在");
            return CommonResult.error(EmErrorCode.SAVE_ERROR+"新增字典'" + dictType.getDictName() + "'失败，字典类型已存在");

        }
        // 调用获取用户名称的方法
//        dictType.setCreateBy(getUserName);
        return CommonResult.ok(dictTypeService.addDictType(dictType));
    }


    /** 
    * @Author gpd
    * @Description 查询字典类型详细(用于字段类型新增和修改)
    * @Date 2023-06-17 上午 09:54
    * @Return CommonResult
    
    */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @ApiOperation(value = "查询字典类型详细(用于字段类型新增和修改)")
    @OptLog(modular = "字典类型管理", type = CommonConstants.SELECT, describe = "查询字典类型详细(用于字段类型新增和修改)")
    @GetMapping(value = "/{dictId}")
    public CommonResult getType(@PathVariable Long dictId){
        return CommonResult.ok(dictTypeService.getType(dictId));
    }

    /**
    * @Author gpd
    * @Description 修改字典类型
    * @Date 2023-06-19 上午 08:36
    * @Return CommonResult

    */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @ApiOperation(value = "修改字典类型")
    @OptLog(modular = "字典类型管理", type = CommonConstants.UPDATE, describe = "修改字典类型")
    @PutMapping("/updateDictType")
    public CommonResult updateDictType(@RequestBody DictType dictType){
        if(!dictTypeService.checkDictTypeUnique(dictType)){
            return CommonResult.error(40001,"修改字典'" + dictType.getDictName() + "'失败，字典类型已存在");
        }
        return CommonResult.ok(dictTypeService.updateDictType(dictType));
    }

    /**
    * @Author gpd
    * @Description 删除字典类型
    * @Date 2023-06-19 上午 09:56
    * @Return CommonResult

    */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @ApiOperation(value = "删除字典类型")
    @OptLog(modular = "字典类型管理", type = CommonConstants.DELETE, describe = "删除字典类型")
    @DeleteMapping("/deleteDictType")
    public CommonResult deleteDictType(Long[] dictIds) throws Exception {
        return dictTypeService.deleteDictType(dictIds);
    }
    
    /** 
    * @Author gpd
    * @Description 刷新字典缓存
    * @Date 2023-06-19 上午 11:13
    * @Return CommonResult
    
    */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @ApiOperation(value = "刷新字典缓存")
    @OptLog(modular = "字典类型管理", type = CommonConstants.UPDATE, describe = "刷新字典缓存")
    @DeleteMapping("/refreshCache")
    public CommonResult refreshCache(){
        return CommonResult.ok(dictTypeService.refreshCache());
    }

    /**
    * @Author gpd
    * @Description 获取字典选择框列表
    * @Date 2023-06-19 下午 02:30
    * @Return CommonResult

    */
    @GetMapping("/optionselect")
    @ApiOperation(value = "获取字典选择框列表")
    @OptLog(modular = "字典类型管理", type = CommonConstants.SELECT, describe = "获取字典选择框列表")
    public CommonResult optionselect(){
        return CommonResult.ok(dictTypeService.optionselect());
    }

    /**
    * @Author gpd
    * @Description 根据前端传回的值查询种植类型
    * @Date 2023-06-26 上午 11:35
    * @Return CommonResult

    */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @ApiOperation(value = "获取种植类型")
    @OptLog(modular = "字典类型管理", type = CommonConstants.SELECT, describe = "获取种植类型")
    @GetMapping("/getPlantType")
    public CommonResult getPlantType(String dictType){
        return CommonResult.ok(dictTypeService.getPlantType(dictType));
    }

    /**
    * @Author gpd
    * @Description 获取教练类型
    * @Date 2023-08-23 上午 11:34
    * @Return CommonResult

    */
    @GetMapping("/getCoachDictType")
    public CommonResult getCoachDictType(){
        return CommonResult.ok(dictTypeService.getCoachDictType());
    }
}
